﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateView3d.h"

#include "ICamera.h"
#include "ICanvas.h"
#include "IDocument.h"
#include "IMainWindow.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IUiView.h"
#include "IUiViewMouseEvent.h"
#include "IUserTransaction.h"
#include "UiDocumentViewUtils.h"
#include "CommandIds.h"
#include "ModelViewUtils.h"
#include "CommandRegister.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdCreateView3d);
CmdCreateView3d::CmdCreateView3d()
    :gcmp::CommandBase(COMMAND_VIEW_3D)
{}

bool CmdCreateView3d::IsEnabled() const
{
    IDocument* pCurrentDoc = UiDocumentViewUtils::GetCurrentDocument();

    return  (pCurrentDoc == nullptr) ? false : true;
}

gcmp::OwnerPtr<gcmp::IAction> CmdCreateView3d::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocument*  pUiDocument = UiDocumentViewUtils::GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocument, L"请打开文档", L"GDMP", L"2023-09-30");

    IUiView* pUiView3d = CreateUiView(pUiDocument, L"View3d");
    if (pUiView3d)
    {
        pUiView3d->SetTitle(pUiDocument->GetDbDocument()->GetName() + L" - 三维");
        pUiView3d->GetCanvas()->Refresh();
#if 0
        pUiView3d->GetUiViewMouseEvent()->Delete(ApplicationEventBus::Get());
        pUiView3d->GetUiViewMouseEvent()->Add(ApplicationEventBus::Get());
#endif
        pUiView3d->GetCanvas()->MarkNeedRefreshByViewScale(true);
        pUiView3d->GetCanvas()->MarkNeedRefreshLineTypeScale(true);
    }

    return gcmp::OwnerPtr<gcmp::IAction>(nullptr);
}


gcmp::IUiView* CmdCreateView3d::CreateUiView(gcmp::IUiDocument* pUiDocument, const std::wstring viewName)
{
    IUiView* pUiView3d = nullptr;
    if (nullptr == pUiDocument)
    {
        return pUiView3d;
    }

    //1.检查打开的视图是否有3D视图
    for each (gcmp::IUiView* pUiView in pUiDocument->GetAllUiViews())
    {
        if (pUiView->IsSubUiView())
        {
            continue;
        }

        if (gcmp::BuiltInViewType::ThreeDimensional != pUiView->GetModelView()->GetViewType())
        {
            continue;
        }

        if (pUiView->GetTag() == viewName)
        {
            pUiView3d = pUiView;
            break;
        }
    }

    //2.有的话直接打开
    if (pUiView3d != nullptr)
    {
        if (gcmp::UiDocumentViewUtils::GetCurrentUiView() != pUiView3d)
        {
            gcmp::IMainWindow::GetMainWindow()->CreateOrOpenUiView(pUiDocument->GetId(), pUiView3d->GetModelView()->GetElementId(), false, pUiView3d->GetTag());
        }
        else
        {
            OwnerPtr<gcmp::IUserTransaction> opUserTransaction = gcmp::IUserTransaction::Create(pUiDocument->GetDbDocument(), L"修改3D模型视图方向");
            IModelView* pModelView3d = pUiView3d->GetModelView();
            pModelView3d->SetViewDirection(Vector3d(1, 1, 1));
            opUserTransaction->Commit();
        }
        return pUiView3d;
    }
    //3.没有的话继续检查文档是否有3D模型
    gcmp::IModelView* pModelView3d = CreateModelView(pUiDocument->GetDbDocument(), L"三维");
    if (nullptr == pModelView3d)
    {
        return nullptr;
    }

    return gcmp::IMainWindow::GetMainWindow()->CreateOrOpenUiView(pUiDocument->GetId(), pModelView3d->GetElementId(), false, viewName);
}


gcmp::IModelView* CmdCreateView3d::CreateModelView(gcmp::IDocument* pDocument, const std::wstring viewName/*L"ModelView3d"*/)
{
    gcmp::IModelView* pModelView3d = nullptr;
    if (nullptr == pDocument)
    {
        return pModelView3d;
    }

    for each (gcmp::IModelView* pModelView in gcmp::IModelView::GetAllModelViews(pDocument))
    {
        if (gcmp::BuiltInViewType::ThreeDimensional == pModelView->GetViewType())
        {
            if (pModelView->GetName() == viewName)
            {
                pModelView3d = pModelView;
                break;
            }
        }
    }

    if (nullptr == pModelView3d)
    {
        if (gcmp::IUserTransaction::IsInUserTransaction(pDocument, nullptr))
        {
            pModelView3d = gcmp::IModelView::Create(pDocument, viewName, gcmp::BuiltInViewType::ThreeDimensional, 
                Vector3d::NegaUnitZ, Vector3d::Zero,
                Vector3d::UnitX, Vector3d::UnitY,
                ElementCreationOptions::Normal);
            if (nullptr != pModelView3d)
            {
                pModelView3d->SetUserDataId(View3DViewType);
            }
            return pModelView3d;
        }

        OwnerPtr<gcmp::IUserTransaction> opUserTransaction = gcmp::IUserTransaction::Create(pDocument, L"创建3D模型视图");
        pModelView3d = IModelView::Create(pDocument, viewName, gcmp::BuiltInViewType::ThreeDimensional, 
            Vector3d::NegaUnitZ, Vector3d::Zero, 
            Vector3d::UnitX, Vector3d::UnitY, 
            ElementCreationOptions::Normal);
        if (nullptr == pModelView3d)
        {
            opUserTransaction->Rollback();
            return nullptr;
        }
        pModelView3d->SetUserDataId(View3DViewType);
        pModelView3d->SetDetailLevel(ModelViewDetailLevel::Fine);
        opUserTransaction->Commit();
    }

    return pModelView3d;
}

